package com.snailhd.common.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.util.Date;

/**
 * @author hd
 * @date 2019-12-10 09:53
 */
public class DateUtils {

    public final static String NOT_SEPARATE_DATETIME = "yyyyMMddHHmmss";

    public final static String DATETIME = "yyyy-MM-dd HH:mm:ss";

    public final static String NOT_SEPARATE_DATE = "yyyyMMdd";

    public final static String DATE = "yyyy-MM-dd";

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String format(Date date){
        return DateFormatUtils.format(date,DATETIME);
    }


    public static String format(Date date,String pattern){
        return DateFormatUtils.format(date,pattern);
    }


    public static Integer formatDate2Int(Date date){
        return Integer.parseInt(format(date,NOT_SEPARATE_DATE));
    }


    public static Long formatDatetime2Long(Date date){
        return Long.parseLong(format(date,NOT_SEPARATE_DATETIME));
    }


    /**
     * 解析yyyy-MM-dd HH:mm:ss 格式的字符串成date
     * @param date 时间字符串
     */
    public static Date parse(String date) throws ParseException {
        return parse(date,DATETIME);
    }


    public static Date parse(String date,String pattern) throws ParseException {
        return org.apache.commons.lang3.time.DateUtils.parseDate(date,pattern);
    }


    public static String today(){
        return format(new Date(),DATE);
    }


    /**
     * 获取两个时间相差秒数
     * @param start 开始时间
     * @param end 结束时间
     * @return 相差秒
     */
    public static long timeDiffSec(Date start,Date end){
        return (end.getTime() - start.getTime())/1000;
    }

    /**
     * 添加时间
     * @param date 时间
     * @param second 添加秒数
     */
    public static Date addSeconds(Date date,Integer second){
        return org.apache.commons.lang3.time.DateUtils.addSeconds(new Date(),second);
    }

    public static Date addMin(Date date,Integer min){
        return addSeconds(date,60 * min);
    }

    public static Date addHour(Date date,Integer hour){
        return addMin(date,60 * hour);
    }

    public static Date addDay(Date date,Integer day){
        return addHour(date,24 * day);
    }
}
